package com.dpj.book.servlet;

import com.alibaba.fastjson.JSONObject;
import com.dpj.book.pojo.OrderBean;
import com.dpj.book.pojo.User;
import com.dpj.book.service.OrderService;
import com.dpj.book.service.impl.OrderServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@WebServlet("/order/*")
public class OrderServlet extends BaseServlet{

    private OrderService orderService=new OrderServiceImpl();

    //结账
    public void checkout(HttpServletRequest request, HttpServletResponse response) throws IOException {
        OrderBean orderBean=new OrderBean();
        Date date=new Date();
        int year = date.getYear();
        int month = date.getMonth();
        int date1 = date.getDate();
        int hours = date.getHours();
        int minutes = date.getMinutes();
        int seconds = date.getSeconds();
        orderBean.setOrderNo(UUID.randomUUID().toString()+"_"+year+month+date1+hours+minutes+seconds);
        orderBean.setOrderDate(date);
        User user = (User) request.getSession().getAttribute("user");
        orderBean.setOrderUser(user);
        orderBean.setOrderMoney(user.getCart().getTotalMoney());
        orderBean.setOrderStatus(0);
        orderService.addOrderBean(orderBean);

    }


    //查看订单列表
    public void getOrderList(HttpServletRequest request, HttpServletResponse response) throws IOException {
        User user = (User) request.getSession().getAttribute("user");
        List<OrderBean> allOrder = orderService.getAllOrder(user);
        // 设置响应数据的中文乱码
        response.setContentType("text/html;charset=utf-8");
        String s = JSONObject.toJSONString(allOrder);
        response.getWriter().write(s);

    }
}
